<template>
	<view>
		<u--form labelPosition="left">
			<u-form-item label="收货人" borderBottom labelWidth="169rpx" labelAlign="center">
				<u--input v-model="userInfo.name" border="none" placeholder="请输入收货人"></u--input>
			</u-form-item>
			<u-form-item label="手机号码" borderBottom labelWidth="169rpx" labelAlign="center">
				<u--input v-model="userInfo.phone" border="none" placeholder="请输入手机号"></u--input>
			</u-form-item>
			<u-form-item label="所在地址" borderBottom labelWidth="169rpx" labelAlign="center">
				<u--input v-model="userInfo.adress" border="none" placeholder="请输入所在地"></u--input>
			</u-form-item>
			<u-form-item label="详细地址" borderBottom labelWidth="169rpx" labelAlign="center">
				<u--textarea v-model="userInfo.alladress" border="none" height="160rpx" placeholder="请输入内容">
				</u--textarea>
			</u-form-item>
			<u-form-item label="标签" borderBottom labelWidth="169rpx" labelAlign="center">
				<!-- 			<u-radio-group  placement="row">
				<u-radio class='rad' @change="radioChange(0)" activeColor="red" label="家"></u-radio>
				<u-radio class='rad' @change="radioChange(1)" activeColor="red" label="公司"></u-radio>
				<u-radio class='rad' @change="radioChange(2)" activeColor="red" label="学校"></u-radio>
			</u-radio-group> -->

				<u-radio-group v-model="userInfo.radiovalue1" placement="row">
					<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item, index) in radiolist1" :key="index"
						:label="item.name" :name="item.name" @change="radioChange">
					</u-radio>
				</u-radio-group>


				<view class="set">
					默认地址
					<u-switch v-model="userInfo.isshow" @change="change"></u-switch>
				</view>
			</u-form-item>
		</u--form>

		<view class="btn">
			<button @click="submit">保存并使用</button>
			<button>重置</button>
		</view>


	</view>
</template>

<script>
	import {
		addressadd,
		addresslist,
		addressedit
	} from '../../rquest/api.js'
	export default {
		data() {
			return {


				radiolist1: [{
						name: '家',
						disabled: false
					},
					{
						name: '公司',
						disabled: false
					},
					{
						name: '学校',
						disabled: false
					}
				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				userInfo: {
					name: '',
					phone: '',
					adress: "",
					alladress: '',
					isshow: false,
					radiovalue1: '家',
				},
			}
		},
		onLoad() {

			if (typeof(uni.getStorageSync('backshow')) == 'number') {
				let a = uni.getStorageSync('backshow')
				let arr = []
				console.log(a);

				addresslist({
					uid: uni.getStorageSync("user").uid,
				}).then(res => {
					console.log(res);
					res.data.list.map(item => {
						if (item.id == a) {
							arr.push(item)
						}
					})
					console.log(arr);
					this.userInfo.name = arr[0].username
					this.userInfo.phone = arr[0].userphone
					this.userInfo.adress = arr[0].location
					this.userInfo.alladress = arr[0].useraddress
					this.userInfo.radiovalue1 = arr[0].label
					this.userInfo.isshow = arr[0].status == 1 ? true : false

					console.log(this.userInfo);
				})

			}
		},
		methods: {
			// sexSelect(e) {
			// 			this.model1.userInfo.sex = e.name
			// 			this.$refs.form1.validateField('userInfo.sex')
			// 		},

			change(e) {
				console.log('change', e);
			},
			submit() {

				if (typeof(uni.getStorageSync('backshow')) == 'number') {
					addressedit({
						id: uni.getStorageSync('backshow'),
						username: this.userInfo.name,
						userphone: this.userInfo.phone,
						location: this.userInfo.adress,
						useraddress: this.userInfo.alladress,
						status: this.userInfo.isshow ? 1 : 2,
						label: this.userInfo.radiovalue1
					}).then(res=>{
						console.log(res);
						if (res.data.code == 200) {
							uni.navigateTo({
								url: '../toaddress/toaddress'
							})
						}
					})
				} else {
					
					if (this.userInfo.name == '') {
						uni.showToast({
							title: '请输入收件人'
						})
						return
					}
					if (this.userInfo.phone == '') {
						uni.showToast({
							title: '请输入手机号'
						})
						return
					}

					if (this.userInfo.adress == '') {
						uni.showToast({
							title: '请输入地址'
						})
						return
					}
					if (this.userInfo.alladress == '') {
						uni.showToast({
							title: '请输入详细地址'
						})
						return
					}

					console.log(this.userInfo);

					addressadd({
						uid: uni.getStorageSync("user").uid,
						username: this.userInfo.name,
						userphone: this.userInfo.phone,
						location: this.userInfo.adress,
						useraddress: this.userInfo.alladress,
						status: this.userInfo.isshow ? 1 : 2,
						label: this.userInfo.radiovalue1
					}).then(res => {
						console.log(res);
						if (res.data.code == 200) {
							uni.navigateTo({
								url: '../toaddress/toaddress'
							})
						}
					})
				}





			},
			radioChange(e) {
				console.log(e);
				this.userInfo.radiovalue1 = e
			}

		}
	}
</script>

<style>
	.u-textarea.data-v-09988a29 {

		background-color: #eee !important;
		padding: 9px;
		color: #333;
	}

	.u-radio-group--row.data-v-97ce24d6 {
		display: flex;
		flex-direction: row;
		justify-content: space-evenly;
	}

	.set {
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
	}

	.btn {
		margin-top: 60rpx;
	}

	button {
		margin-bottom: 20rpx;
	}

	button:nth-child(1) {
		background-color: #FF6040;
		color: #fff;
		font-weight: bold;
		width: 90%;
	}

	button:nth-child(2) {
		font-weight: bold;
		width: 90%;
	}
</style>
